package cn.jimmie.lib.qlog;


import android.content.Context;

import java.util.ArrayList;
import java.util.List;

import cn.jimmie.lib.qlog.interceptor.ConsoleInterceptor;
import cn.jimmie.lib.qlog.interceptor.FileInterceptor;
import cn.jimmie.lib.qlog.interceptor.HeaderInterceptor;
import cn.jimmie.lib.qlog.interceptor.Interceptor;
import cn.jimmie.lib.qlog.interceptor.NetworkInterceptor;

/**
 * Func :
 *
 * <p> Create Time:  2019-12-18 10:53 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public final class QLog {
    private static final Proxy PROXY = new QLogProxy();

    public static void init(Config conf) {
        PROXY.init(conf);
    }

    public static void release() {
        PROXY.release();
    }

    public static void v(String source, Object... args) {
        PROXY.v(QFormat.DEFAULT, source, args);
    }

    public static void d(String source, Object... args) {
        PROXY.d(QFormat.DEFAULT, source, args);
    }

    public static void i(String source, Object... args) {
        PROXY.i(QFormat.DEFAULT, source, args);
    }

    public static void w(String source, Object... args) {
        PROXY.w(QFormat.DEFAULT, source, args);
    }

    public static void e(String source, Object... args) {
        PROXY.e(QFormat.DEFAULT, source, args);
    }

    public static void e(Throwable t) {
        PROXY.e(t);
    }

    public static void v(QFormat format, String source, Object... args) {
        PROXY.v(format, source, args);
    }

    public static void d(QFormat format, String source, Object... args) {
        PROXY.d(format, source, args);
    }

    public static void i(QFormat format, String source, Object... args) {
        PROXY.i(format, source, args);
    }

    public static void w(QFormat format, String source, Object... args) {
        PROXY.w(format, source, args);
    }

    public static void e(QFormat format, String source, Object... args) {
        PROXY.e(format, source, args);
    }

    public static class Config {
        List<Interceptor> interceptors;
        HeaderInterceptor headerInterceptor;
        Context appCtx;
        String tag;
        QLogLevel level;
        String logPath;
        long logMaxSize;

        public static Config creator(Context appCtx) {
            return new Config(appCtx.getApplicationContext());
        }

        private Config(Context appCtx) {
            this.appCtx = appCtx;
        }

        public Config level(QLogLevel level) {
            this.level = level;
            return this;
        }

        public Config tag(String tag) {
            this.tag = tag;
            return this;
        }

        public Config logPath(String path) {
            this.logPath = path;
            return this;
        }

        public Config logMaxSize(long size) {
            this.logMaxSize = size;
            return this;
        }

        public Config networkInterceptor(NetworkInterceptor interceptor) {
            networkInterceptor(interceptor, true);
            return this;
        }

        public Config networkInterceptor(NetworkInterceptor interceptor, boolean enable) {
            if (interceptor == null) return this;
            if (interceptors == null) interceptors = new ArrayList<>();
            interceptor.enabled(enable);
            interceptors.add(interceptor);
            return this;
        }

        public Config fileInterceptor(FileInterceptor interceptor) {
            fileInterceptor(interceptor, true);
            return this;
        }

        public Config fileInterceptor(FileInterceptor interceptor, boolean enable) {
            if (interceptor == null) return this;
            if (interceptors == null) interceptors = new ArrayList<>();
            interceptor.enabled(enable);
            interceptors.add(interceptor);
            return this;
        }

        public Config consoleInterceptor(ConsoleInterceptor interceptor) {
            consoleInterceptor(interceptor, true);
            return this;
        }

        public Config consoleInterceptor(ConsoleInterceptor interceptor, boolean enable) {
            if (interceptor == null) return this;
            if (interceptors == null) interceptors = new ArrayList<>();
            interceptor.enabled(enable);
            interceptors.add(interceptor);
            return this;
        }

        public Config headerInterceptor(HeaderInterceptor interceptor) {
            this.headerInterceptor = interceptor;
            return this;
        }
    }
}
